﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ROrganizer.Client;
using ROrganizer.Client.Files;
using ROrganizer.UI.Controllers.Core;

namespace ROrganizer.UI.Controllers
{
    internal class CopySettingsFormController
    {
        private readonly string[] files;
        private readonly ImdbRepository repository;
        private readonly CopySettings settings;
        private CopyTask[] tasks;

        public CopySettingsFormController(string[] files)
        {
            this.files = files;
            this.repository = new ImdbRepository();
            this.settings = ApplicationSettings.GetInstance().DefaultCopySettings;

            this.Files = new FastObservableCollection<FileEntry>();
            this.RenameUsingImdb = true;

            this.Reload();
        }

        public CopyTask[] GetTasks()
        {
            return this.tasks;
        }

        private void Reload()
        {
            var processor = new FileProcessor();
            var query = from task in processor.PrepareToCopy(files, this.settings)
                        select new FileEntry
                        {
                            Path = task.Source,
                            Name = task.DestinationName,
                            Size = FileEnumerator.GetFileSize(task.Source),
                            IsConflicted = task.IsConflicted,
                            Reference = task,
                        };

            this.Files.Clear();
            this.Files.AddRange(query.ToArray());
        }

        public void Refresh()
        {
            this.Reload();
        }

        public bool RenameUsingImdb
        {
            get { return this.settings.RenameUsingImdbTitles; }
            set { this.settings.RenameUsingImdbTitles = value; }
        }

        public string DestinationPath
        {
            get { return this.settings.DestinationFolder; }
        }

        public FastObservableCollection<FileEntry> Files { get; private set; }

        public bool CanBrowseFolder()
        {
            return true;
        }

        public void BrowseFolder(IWin32Window owner)
        {
            this.settings.DestinationFolder = ViewManager.ChooseFolder(owner, "Choose destination folder for your movies", this.DestinationPath) ?? this.DestinationPath;
        }

        public bool CanCommit()
        {
            return !String.IsNullOrEmpty(this.DestinationPath)
                && Directory.Exists(this.DestinationPath)
                && this.Files.All(x => !x.IsConflicted);
        }

        public void Commit()
        {
            var settings = ApplicationSettings.GetInstance();
            settings.DefaultCopySettings = this.settings;

            ApplicationSettings.Persist(settings);
            this.tasks = this.Files.Select(x => x.Reference).ToArray();
        }

        public void PlayMovie(FileEntry entry)
        {
            if (entry == null || !File.Exists(entry.Path))
                return;

            Process.Start(entry.Path);
        }

        public class FileEntry
        {
            public string Path { get; set; }
            public string Name { get; set; }
            public long Size { get; set; }
            public bool IsConflicted { get; set; }
            public CopyTask Reference { get; set; }
        }
    }
}
